package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-21
 * Time: 15:27
 */
public class ThreadDemon21 {
    private static long count = 0;
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();//创建一个object对象

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                synchronized (object){  //锁到object这个对象上
                    count++;
                }
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                synchronized(object){   //锁到object这个对象上
                    count++;
                }

            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println("计算结果是：" + count);
    }

}
